//
// Created by Tony on 2024/7/10.
//

#include "TVM.h"
#include "instructions/InstructionFactory.h"
#include <memory>

namespace Runtime {

using std::make_shared;

TVM::TVM() {
    mClassTable = make_shared<RtCode::ClassTable>();
    mClassTable->init();
    InstructionFactory::init();
}

shared_ptr<RtData::Frame> TVM::interpretStatement(const string& code) {
    return Interpreter::interpretStatement(mClassTable, code);
}

shared_ptr<RtData::Frame> TVM::interpretModule(const string& fileName) {
    auto sourceFile = fileUtil.readSourceFile(fileName);
    FileUtil::printSourceFile(sourceFile); // for debug
    return interpretModule(sourceFile);
}

shared_ptr<RtData::Frame> TVM::interpretModule(const shared_ptr<SourceFile>& sourceFile) {
    return Interpreter::interpretModule(mClassTable, sourceFile);
}

shared_ptr<RtData::Frame> TVM::interpretClass(const string& fileName) {
    auto sourceFile = fileUtil.readSourceFile(fileName);
    FileUtil::printSourceFile(sourceFile); // for debug
    return interpretClass(sourceFile);
}

shared_ptr<RtData::Frame> TVM::interpretClass(const shared_ptr<SourceFile>& sourceFile) {
    return Interpreter::interpretClass(mClassTable, sourceFile);
}

} // namespace Runtime
